package gui.editpatient;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;

import mchc.project.triageetagandroid.R;

import cdautils.TriageTag.BodyPart;
import gui.defineinjuries.BodyPartTranslator;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class InjuriesDataAdapter extends BaseAdapter {

	Context mContext;
	private String[] bodypart;
	private String[] verletzung;

	private LayoutInflater mInflater;

	public InjuriesDataAdapter(Context c,
			HashMap<BodyPart, String> verletzungen) {
		mContext = c;
		mInflater = LayoutInflater.from(c);

		Set<Entry<BodyPart, String>> entryset = verletzungen.entrySet();
		Iterator<Entry<BodyPart, String>> it = entryset.iterator();

		ArrayList<String> bodypartArrayList = new ArrayList<String>();
		ArrayList<String> verletzungenArrayList = new ArrayList<String>();

		while (it.hasNext()) {
			Entry<BodyPart, String> entry = it.next();
			bodypartArrayList.add(BodyPartTranslator
					.translateBodyPartToReadableName(entry.getKey()) + ":");
			verletzungenArrayList.add(entry.getValue().trim());
		}

		bodypart = new String[bodypartArrayList.size()];
		bodypart = bodypartArrayList.toArray(bodypart);

		verletzung = new String[verletzungenArrayList.size()];
		verletzung = verletzungenArrayList.toArray(verletzung);
	}

	public int getCount() {
		return bodypart.length;
	}

	public Object getItem(int position) {
		return bodypart[position];
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;

		if (convertView == null || convertView.getTag() == null) {
			convertView = mInflater.inflate(R.layout.grid_verletzungen, parent,
					false);
			
			holder = new ViewHolder();
			holder.txtId = (TextView) convertView.findViewById(R.id.txtId);
			holder.txtId.setPadding(0, 10, 10 , 10);

			holder.txtName = (TextView) convertView.findViewById(R.id.txtName);
			holder.txtName.setPadding(0, 0, 10, 0);

			if (position == 0) {
				convertView.setTag(holder);
			}
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.txtId.setText(bodypart[position]);
		holder.txtName.setText(verletzung[position]);

		return convertView;
	}

	static class ViewHolder {
		TextView txtId;
		TextView txtName;
	}

}
